<?php
/**
*
* @package ppkBB3cker
* @version $Id: index_add1.php 1.000 2009-04-25 19:26:00 PPK $
* @copyright (c) 2009 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$dt=time();
$ppkbb_tstat_ctime=$config['ppkbb_tstat_ctime'];
$total_torrents = $config['ppkbb_num_torrents'];
$total_comments = $config['ppkbb_num_comments'];
$ppkbb_tcenable_rannounces=my_split_config($config['ppkbb_tcenable_rannounces'], 9, 'my_int_val');
$l_total_torrent_s = ($total_torrents == 0) ? 'TOTAL_TORRENT_ZERO' : 'TOTAL_TORRENT_OTHER';
$l_total_comment_s = ($total_comments == 0) ? 'TOTAL_COMMENT_ZERO' : 'TOTAL_COMMENT_OTHER';
$total_rem_peers=$total_rem_leech=$total_rem_seed=0;
if($ppkbb_tcenable_rannounces[0])
{
	$sql="SELECT SUM(seeders) seeder, SUM(leechers) leecher FROM ".TRACKER_RANNOUNCES_TABLE."";
	$result=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
	$total_rem_peer=$db->sql_fetchrow($result);
	$total_rem_peers=my_int_val($total_rem_peer['seeder']+$total_rem_peer['leecher']);
	$total_rem_seed=my_int_val($total_rem_peer['seeder']);
	$total_rem_leech=my_int_val($total_rem_peer['leecher']);
}

$sql="SELECT SUM(IF(seeder='yes',1,0)) seeder, SUM(IF(seeder='no',1,0)) leecher FROM ".TRACKER_PEERS_TABLE."";
$result=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
$total_peer=$db->sql_fetchrow($result);
$total_peers=my_int_val($total_peer['seeder']+$total_peer['leecher']+$total_rem_peers);
$l_total_peer_s = ($total_peers == 0) ? 'TOTAL_PEER_ZERO' : 'TOTAL_PEER_OTHER';
$total_seed=my_int_val($total_peer['seeder']+$total_rem_seed);
$l_total_seed_s = ($total_seed == 0) ? 'TOTAL_SEED_ZERO' : 'TOTAL_SEED_OTHER';
$total_leech=my_int_val($total_peer['leecher']+$total_rem_leech);
$l_total_leech_s = ($total_leech == 0) ? 'TOTAL_LEECH_ZERO' : 'TOTAL_LEECH_OTHER';

$sql="SELECT SUM(uploaded) upload, SUM(downloaded) download FROM ".TRACKER_SNATCHED_TABLE."";
$result=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
$total_updown=$db->sql_fetchrow($result);
$total_up=my_int_val($total_updown['upload']);
$l_total_up_s = ($total_up == 0) ? 'TOTAL_UP_ZERO' : 'TOTAL_UP_OTHER';
$total_down=my_int_val($total_updown['download']);
$l_total_down_s = ($total_down == 0) ? 'TOTAL_DOWN_ZERO' : 'TOTAL_DOWN_OTHER';

$sql="SELECT SUM(uploaded) upload, SUM(downloaded) download FROM ".TRACKER_PEERS_TABLE."";
$result=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
$total_supdown=$db->sql_fetchrow($result);
$total_sup=my_int_val($total_supdown['upload']);
$l_total_sup_s = ($total_sup == 0) ? 'TOTAL_SUP_ZERO' : 'TOTAL_SUP_OTHER';
$total_sdown=my_int_val($total_supdown['download']);
$l_total_sdown_s = ($total_sdown == 0) ? 'TOTAL_SDOWN_ZERO' : 'TOTAL_SDOWN_OTHER';

$sql="SELECT SUM(size) size FROM ".TRACKER_FILES_TABLE."";
$result=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
$total_size=$db->sql_fetchrow($result);
$total_sizes=my_int_val($total_size['size']);
$l_total_sizes_s = ($total_sizes == 0) ? 'TOTAL_SIZE_ZERO' : 'TOTAL_SIZE_OTHER';

$sql="SELECT COUNT(DISTINCT(torrent)) seeders, COUNT(DISTINCT(userid)) seeders2 FROM ".TRACKER_PEERS_TABLE." where seeder='yes'";
$result=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
$total_t_seeds=$db->sql_fetchrow($result);
$t_seed=my_int_val($total_t_seeds['seeders']);
$sql="SELECT COUNT(DISTINCT(torrent)) leechers, COUNT(DISTINCT(userid)) leechers2 FROM ".TRACKER_PEERS_TABLE." where seeder='no'";
$result=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
$total_t_leechs=$db->sql_fetchrow($result);
$t_leech=my_int_val($total_t_leechs['leechers']);
$l_t_down_s = ($t_seed == 0) ? 'TOTAL_TDOWN_ZERO' : 'TOTAL_TDOWN_OTHER';
$l_t_up_s = ($t_leech == 0) ? 'TOTAL_TUP_ZERO' : 'TOTAL_TUP_OTHER';
$u_seed=my_int_val($total_t_seeds['seeders2']);
$u_leech=my_int_val($total_t_leechs['leechers2']);
$l_u_down_s = ($u_seed == 0) ? 'TOTAL_UDOWN_ZERO' : 'TOTAL_UDOWN_OTHER';
$l_u_up_s = ($u_leech == 0) ? 'TOTAL_UUP_ZERO' : 'TOTAL_UUP_OTHER';

if($dt - $config['ppkbb_trlast_stattime'] > $ppkbb_tstat_ctime * 60)
{
	$sql="SELECT SUM(p.uploadoffset/(p.last_action-p.prev_action)) up_speed, SUM(p.downloadoffset/(p.last_action-p.prev_action)) down_speed FROM ".TRACKER_PEERS_TABLE." p WHERE {$dt}-p.last_action < {$config['ppkbb_tcdead_time']}";
	$result=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
	$total_updown_speed=$db->sql_fetchrow($result);
	$up_speed=my_int_val($total_updown_speed['up_speed']);
	$down_speed=my_int_val($total_updown_speed['down_speed']);
	$result=$db->sql_query("UPDATE ".CONFIG_TABLE ." SET config_value='$dt' WHERE config_name='ppkbb_trlast_stattime'");
	$result=$db->sql_query("UPDATE ".CONFIG_TABLE ." SET config_value='$up_speed $down_speed' WHERE config_name='ppkbb_trstat_vals'");
}
else
{
	list($up_speed, $down_speed, )=@explode(' ', $config['ppkbb_trstat_vals']);
}
$l_up_speed = !$up_speed ? 'TOTAL_SPEEDUP_ZERO' : 'TOTAL_SPEEDUP_OTHER';
$l_down_speed = !$down_speed ? 'TOTAL_SPEEDDOWN_ZERO' : 'TOTAL_SPEEDDOWN_OTHER';
$up_speed=get_formatted_filesize($up_speed, 1, false, 1);
$down_speed=get_formatted_filesize($down_speed, 1, false, 1);
?>
